package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Doctor;
import com.kkb.service.DoctorService;
import com.kkb.vo.DoctorQueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.print.Doc;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("doctors")
@ResponseBody
public class DoctorController {
    @Resource
    DoctorService doctorService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO list(int index, int size, DoctorQueryVO doctorQueryVO) {
        PageInfo pageInfo = doctorService.queryAll(index, size, doctorQueryVO);
        return new ResultVO(pageInfo);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO queryById(@PathVariable("id") Integer id) {
        Doctor doctor = doctorService.queryById(id);
        System.out.println(doctor.getBirthday());
        if (doctor != null)
            return new ResultVO(doctor);

        return new ResultVO(404, "未找到此医生");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO update(@PathVariable("id") Integer id, Doctor doctor) {
        doctor.setId(id);
        int result = doctorService.update(doctor);
        return result == 1 ? new ResultVO("更新成功!") : new ResultVO(404, "更新失败!");
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO add(Doctor doctor) {
        doctor.setState(0);
        doctor.setInTime(new Date());
        int result = doctorService.add(doctor);
        return result == 1 ? new ResultVO("添加成功!") : new ResultVO(500, "添加失败!");
    }

    @RequestMapping(value = "department/{departmentId}", method = RequestMethod.GET)
    public ResultVO selectByDepartmentId(@PathVariable("departmentId") Integer departmentId){
        List<Doctor> doctors = doctorService.selectByDepartmentId(departmentId);
        return doctors != null ? new ResultVO(doctors):new ResultVO(404,"暂无信息");
    }
}
